home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #45 (Jun 89) / Splitbar Test ƒ / Test_Window.Pas < prev   
Pascal/Delphi Source File  |  1989-03-28  |  8KB  |  224 lines

  1. unit Test_Window;
  2.  
  3. {File name: Test_Window.Pas}
  4. {Function: Handle a Window}
  5. {History: 3/14/89 Original by Prototyper.   }
  6.  
  7. interface
  8.     uses
  9.         MyGlobals, MyScroll;
  10.  
  11.     procedure Close_Test_Window (whichWindow: WindowPtr); {Close our window}
  12.     procedure Open_Test_Window;  {Open our window and draw everything}
  13.     procedure Update_Test_Window; {Update our window, someone uncovered a part of us}
  14.     procedure Do_Test_Window (myEvent: EventRecord); {Handle action to our window, like controls}
  15.  
  16. implementation
  17. {=================================}
  18.     procedure Close_Test_Window; {Close our window}
  19.     begin
  20.         if (MyWindow <> nil) and (MyWindow = whichWindow) then {Close if this is us}
  21.             begin
  22.                 DisposeWindow(MyWindow);          {Clear window and controls}
  23.                 MyWindow := nil;
  24.                 TEDispose(EditText); {dispose of TE record}
  25.                 EditText := nil;
  26.             end;                              {End for if (MyWindow<>nil)}
  27.     end; {of Close_Test_Window}
  28.  
  29.     procedure UpDate_Test_Window; {Update our window, someone uncovered a part of us}
  30.     begin
  31.         PenNormal;
  32.         EraseRect(paneRect); {redraw dividing lines between two panes if any}
  33.         SetRect(paneRect, 0, Split^^.contrlValue + 1, Split^^.contrlRect.left, Split^^.contrlValue + 5);
  34.         if ((Split^^.contrlValue > 0) and (Split^^.contrlValue < Split^^.contrlMax)) then {is there enough room?}
  35.             begin
  36.                 FrameRect(paneRect);
  37.             end;
  38.  
  39.         if (EditText <> nil) then {update EditText to both panes}
  40.             begin
  41.                 TEDeactivate(EditText);
  42.                 SetEditText(1);
  43.                 TEUpdate(MyWindow^.portRect, EditText); {Update Lower pane}
  44.                 SetEditText(2);
  45.                 TEUpdate(MyWindow^.portRect, EditText); {Update Upper pane}
  46.                 SetEditText(activePane);
  47.                 TEActivate(EditText);
  48.             end;
  49.  
  50.         DrawControls(MyWindow);           {Draw all the controls}
  51.         DrawGrowIcon(MyWindow);           {Draw the Grow box}
  52.     end;  {of Update_Test_Window}
  53.  
  54.     procedure Open_Test_Window; {Open our window and draw everything}
  55.         var
  56.             sTemp: str255; { For initializing the text window}
  57.     begin                                 {Start of Window open routine}
  58.         if (MyWindow = nil) then            {Handle an open when already opened}
  59.             begin
  60.                 MyWindow := GetNewWindow(WindowID, nil, Pointer(-1)); {Get the window from the resource file}
  61.                 SelectWindow(MyWindow);           {Bring our window to the front}
  62.                 SetPort(MyWindow);                {Prepare to write into our window}
  63.  
  64.      { Make a scroll bars}
  65.                 Scroll[2] := GetNewControl(Scrollbar2, MyWindow); {Make a new scrollbar}
  66.                 Scroll[1] := GetNewControl(Scrollbar1, MyWindow); {Make a new scrollbar}
  67.         { Make a splitbar}
  68.                 Split := GetNewControl(Splitbar, MyWindow); {Make a new scrollbar}
  69.  
  70.         {Set max/min and initial values of scrollbars}
  71.                 SetCtlMax(Scroll[1], 0);
  72.                 SetCtlMin(Scroll[1], 0);
  73.                 SetCtlValue(Scroll[1], 0);
  74.                 SetCtlMax(Scroll[2], 0);
  75.                 SetCtlMin(Scroll[2], 0);
  76.                 SetCtlValue(Scroll[2], 0);
  77.                 FixScrollbarRects; {redraw scroll/split bars done}
  78.  
  79.                 FixTextRects; {get TE record's rectangles for switching}
  80.  
  81.                 activePane := 1; {Set active pane}
  82.  
  83.                 EditText := TENew(viewRect[1], destRect[1]);
  84.                 HLock(Handle(EditText)); { set up attributes of of TE record}
  85.                 with EditText^^ do
  86.                     begin
  87.                         txFont := applFont;
  88.                         fontAscent := 12;
  89.                         lineHeight := 12 + 3 + 1;
  90.                     end;
  91.                 HUnLock(Handle(EditText));
  92.                 sTemp := 'Splitbar available for those with split personalities';
  93.                 TESetText(Pointer(Ord4(@sTemp) + 1), length(sTemp), EditText);
  94.                 FixTextRects; {get TE record's rectangles for switching}
  95.                 TEActivate(EditText);
  96.                 ShowWindow(MyWindow);
  97.                 UpDate_Test_Window;   {Do an update to draw rest of items}
  98.             end                               {End for if (MyWindow<>nil)}
  99.         else
  100.             SelectWindow(MyWindow);           {Already open, so show it}
  101.     end;  {of Open_Test_Window}
  102.  
  103.     procedure HandleScrollBars (theControl: ControlHandle; thePart: integer); {scroll Text while in scrollbar}
  104.         var
  105.             oldValue, delta, pane: integer;               {Value of the scrollbar}
  106.     begin
  107.         case GetCRefCon(theControl) of {find which pane}
  108.             ScrollBar1: 
  109.                 pane := 1;
  110.             ScrollBar2: 
  111.                 pane := 2;
  112.         end;
  113.  
  114.         case thePart of {get delta}
  115.             inUpButton: 
  116.                 delta := -1;
  117.             inDownButton: 
  118.                 delta := 1;
  119.             inPageUp: 
  120.                 delta := -(viewRect[pane].bottom - viewRect[pane].top) div EditText^^.lineHeight;
  121.             inPageDown: 
  122.                 delta := (viewRect[pane].bottom - viewRect[pane].top) div EditText^^.lineHeight;
  123.         end;
  124.  
  125.         if thePart <> 0 then {set Control's value and adjust text}
  126.             begin
  127.                 oldValue := GetCtlValue(theControl);
  128.                 SetCtlValue(theControl, oldValue + delta);
  129.                 AdjustText(pane);
  130.             end;
  131.     end; {of HandleScrollBars}
  132.  
  133.     procedure Do_Test_Window; {Handle action to our window, like controls}
  134.         var
  135.             RefCon: integer;                     {RefCon for controls}
  136.             code: integer;                       {Location of event in window or controls}
  137.             theValue: integer;                   {Current value of a control}
  138.             whichWindow: WindowPtr;              {Window pointer where event happened}
  139.             myPt: Point;                         {Point where event happened}
  140.             theControl: ControlHandle;           {Handle for a control}
  141.             extend: boolean; {TE extending with Shift key modifier}
  142.  
  143.         procedure Do_A_ScrollBar (code: integer); {Handle a ScrollBar being pressed}
  144.         begin                                 {Handle a ScrollBar being pressed}
  145.             RefCon := GetCRefCon(theControl);   {get control refcon}
  146.  
  147.             case RefCon of                      {Select correct scrollbar}
  148.                 Splitbar:               {Splitbar}
  149.                     begin                     {start for this scroll bar}
  150.                         TEDeactivate(EditText);
  151.                         code := TrackControl(theControl, myPt, nil); {Let the OS drag it around}
  152.                         FixScrollbarRects;
  153.                         TEActivate(EditText);
  154.                     end;                      {end for this scroll bar}
  155.  
  156.                 Scrollbar1:{Scrollbars}
  157.                     begin
  158.                   {start for this scroll bar}
  159.                         if code <> inThumb then
  160.                             code := TrackControl(theControl, myPt, @HandleScrollBars)
  161.                         else
  162.                             begin
  163.                                 code := TrackControl(theControl, myPt, nil);
  164.                                 AdjustText(1);
  165.                             end;
  166.                     end;                      {end for this scroll bar}
  167.  
  168.                 Scrollbar2:               {Scrollbars}
  169.                     begin                     {start for this scroll bar}
  170.                         if code <> inThumb then
  171.                             code := TrackControl(theControl, myPt, @HandleScrollBars)
  172.                         else
  173.                             begin
  174.                                 code := TrackControl(theControl, myPt, nil);
  175.                                 AdjustText(2);
  176.                             end;
  177.                     end;                      {end for this scroll bar}
  178.             end;                                {end of case}
  179.         end;                                  {Handle a ScrollBar being pressed}
  180.  
  181.     begin                                 {Start of Window handler}
  182.         if (MyWindow <> nil) then           {Handle only when the window is valid}
  183.             begin
  184.                 code := FindWindow(myEvent.where, whichWindow); {Get where in window and which window}
  185.  
  186.                 if (myEvent.what = MouseDown) and (MyWindow = whichWindow) then { convert coords }
  187.                     begin                           {}
  188.                         myPt := myEvent.where;          {Get mouse position}
  189.                         GlobalToLocal(myPt);
  190.                     end;
  191.  
  192.                 if (MyWindow = whichWindow) and (code = inContent) then {for our window}
  193.                     begin
  194.                         TEDeactivate(EditText); {remove hilighting}
  195.                         if PtInRect(MyPt, viewRect[1]) then {determine which pane mouse down in}
  196.                             begin
  197.                                 SetEditText(1);
  198.                                 activePane := 1;
  199.                             end
  200.                         else if PtInRect(MyPt, viewRect[2]) then
  201.                             begin
  202.                                 SetEditText(2);
  203.                                 activePane := 2;
  204.                             end;
  205.                         TEActivate(EditText); {put highlighting back}
  206.  
  207.                         if PtInRect(MyPt, EditText^^.viewRect) then {do click routine for TE record}
  208.                             begin
  209.                                 extend := (BitAnd(myEvent.modifiers, ShiftKey) <> 0);
  210.                                 TEClick(myPt, extend, EditText);
  211.                             end;
  212.  
  213.                         code := FindControl(myPt, whichWindow, theControl); {Get type of control}
  214.                         if (code <> 0) then             {Check type of control}
  215.                             code := TrackControl(theControl, myPt, nil); {Track the control}
  216.  
  217.                         if (code = inUpButton) or (code = inDownButton) or (code = inThumb) or (code = inPageDown) or (code = inPageUp) then
  218.                             Do_A_ScrollBar(code);         {Do scrollbars}
  219.  
  220.                     end;                            {End for if (MyWindow=whichWindow)}
  221.             end;                              {End for if (MyWindow<>nil)}
  222.     end;                                  {End of procedure}
  223.  
  224. end.                                    {End of unit}